package com.adinnet.admin.dict.controller;


import com.adinnet.admin.dict.model.Dict;
import com.adinnet.admin.dict.model.form.DictForm;
import com.adinnet.admin.dict.service.DictService;
import com.adinnet.common.utils.StringUtil;
import com.adinnet.core.BaseController;
import com.adinnet.core.JsonResult;
import com.adinnet.core.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 * 字典 前端控制器
 * </p>
 *
 * @author sunjianbo
 * @since 2019-11-05
 */
@RestController
@RequestMapping("/admin/dict")
public class DictController extends BaseController {
    @Autowired
    private DictService dictService;

    @GetMapping
    public ModelAndView dict(Model model){
        return new ModelAndView("dict/dict.html");
    }
    /**
     * 列表
     *
     * @param form
     * @return
     */
    @GetMapping("/list")
    public PageResult<Dict> list(DictForm form) {
        if (StringUtils.isEmpty(form.getDictCode())) {
            return new PageResult("编码不能为空");
        }
        return dictService.queryList(form);
    }

    /**
     * 获取字典列表
     *
     * @param form
     * @return
     */
    @GetMapping("/dictList")
    public PageResult<Dict> dictList(DictForm form) {
        return dictService.dictList(form);
    }
    /**
     * 不分页列表
     *
     * @param dict
     * @return
     */
    @GetMapping("/noPageList")
    public PageResult<Dict> noPageList(Dict dict) {
        if (StringUtils.isEmpty(dict.getDictCode())) {
            return new PageResult("编码不能为空");
        }
        return dictService.noPageList(dict);
    }

    /**
     * 新增dict
     *
     * @param dict
     * @return
     */
    @ResponseBody
    @PostMapping("/add")
    public JsonResult addDict(Dict dict) {
        if (null == dict || "".equals(dict)) {
            return JsonResult.error("请输入参数");
        }
        return dictService.addDict(dict);
    }

    /**
     * 修改dict
     *
     * @param dict
     * @return
     */
    @ResponseBody
    @PostMapping("/update")
    public JsonResult updateDict(Dict dict) {
        if (null == dict || "".equals(dict)) {
            return JsonResult.error("请输入参数");
        }
        return dictService.updateDict(dict);
    }


    @ResponseBody
    @PostMapping("/updateBootPage")
    public JsonResult updateBootPage(int checked) {

        return dictService.updateBootPage(checked);
    }

    /**
     * 删除
     *
     * @param dictId
     * @return
     */
    @PostMapping("/delete")
    public JsonResult deleteDict(String dictId) {
        if (StringUtil.isBlank(dictId)) {
            return JsonResult.error("删除失败");
        }
        return dictService.deleteDict(dictId);
    }

}
